package com.irdstudio.bsp.executor.core.plugin.migrate.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移校验指标类-[表名: 迁移校验指标(migrate_check_index)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-04
 */
public class MigrateCheckIndexDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateCheckIndexDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateCheckIndex> queryMigrateCheckIndexWithAll()
			throws SQLException {
		List<MigrateCheckIndex> dcList = new ArrayList<MigrateCheckIndex>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		MigrateCheckIndex dc = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM migrate_check_index");
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new MigrateCheckIndex();
				dc.setCheckIndexId(rs.getString("check_index_id"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setCheckIndexName(rs.getString("check_index_name"));
				dc.setCheckIndexLevel(rs.getString("check_index_level"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setSourceIndexSql(rs.getString("source_index_sql"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dc.setTargetIndexSql(rs.getString("target_index_sql"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateCheckIndexWithAll is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateCheckIndex> queryMigrateCheckIndexWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<MigrateCheckIndex> dcList = new ArrayList<MigrateCheckIndex>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM migrate_check_index "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				MigrateCheckIndex dc = new MigrateCheckIndex();
				dc.setCheckIndexId(rs.getString("check_index_id"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setCheckIndexName(rs.getString("check_index_name"));
				dc.setCheckIndexLevel(rs.getString("check_index_level"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setSourceIndexSql(rs.getString("source_index_sql"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dc.setTargetIndexSql(rs.getString("target_index_sql"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateCheckIndexWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}